package com.hnchances.controller;

import com.github.pagehelper.PageInfo;
import com.hnchances.entity.Grades;
import com.hnchances.service.ScoreService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/scores") // 建议用复数
public class ScoreController {

    @Autowired
    private ScoreService scoreService;

    @PostMapping
    public String add(@RequestBody Grades grades) {
        scoreService.addScore(grades);
        return "新增成功";
    }

    @PutMapping
    public String update(@RequestBody Grades grades) {
        scoreService.updateScore(grades);
        return "修改成功";
    }

    @DeleteMapping("/{id}")
    public String delete(@PathVariable Long id) {
        scoreService.deleteScore(id);
        return "删除成功";
    }

    @GetMapping("/{id}")
    public Grades getById(@PathVariable Long id) {
        return scoreService.getScoreById(id);
    }

    @GetMapping
    public PageInfo<Map<String, Object>> query(@RequestParam(required = false) String studentName,
                                               @RequestParam(required = false) String className,
                                               @RequestParam(required = false) String courseName,
                                               @RequestParam(required = false) String examName,
                                               @RequestParam(defaultValue = "1") int pageNum,
                                               @RequestParam(defaultValue = "10") int pageSize) {
        return scoreService.queryScores(studentName, className, courseName, examName, pageNum, pageSize);
    }
}